package org.example.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.domain.NitRecord;
import org.example.domain.User;

@Mapper
public interface NitRecordMapper {

    @Insert("INSERT INTO nit_record (userId,date,content,qq) VALUES (#{userId},#{date},#{content},#{qq});")
    void insertOneNitRecord(NitRecord nitRecord);

    @Select("SELECT * FROM nit_record WHERE userId=#{userId} ORDER BY date DESC;")
    NitRecord[] selectNitRecordsByUserId(Integer userId);

    @Select("<script>SELECT * FROM nit_record WHERE " +
            "<if test=\"isAnswer==0\"> answer IS null </if>" +
            "<if test=\"isAnswer==1\"> answer IS NOT null AND adminId=#{adminId} ORDER BY date DESC LIMIT 100</if>" +
            ";</script>")
    @Results({
            @Result(
                column = "userId",
                property = "user",
                javaType= User.class,
                one = @One(select="org.example.Mapper.UserMapper.getAllById")
            )
    })
    NitRecord[] selectNitRecordsByAnswer(@Param("isAnswer") Integer isAnswer,@Param("adminId") Integer adminId);

    @Select("SELECT * FROM nit_record WHERE id=#{id} LIMIT 1;")
    NitRecord selectNitRecordById(Integer id);

    @Update("UPDATE nit_record SET answer=#{answer},adminId=#{adminId} WHERE id=#{id};")
    void answerNitRecordById(NitRecord nitRecord);

}
